/**
 * 
 */
package com.thor.palmmall.controller.statement;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thor.commons.entity.UCN;
import com.thor.commons.exception.ThorServiceException;
import com.thor.palmmall.spi.basic.SBasicService;
import com.thor.palmmall.spi.basic.SBuildingsParam;
import com.thor.palmmall.spi.basic.SFloorsParam;
import com.thor.palmmall.spi.brand.SBizType;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.statement.SQueryByContractParam;
import com.thor.palmmall.spi.statement.SQueryByStoreParam;
import com.thor.palmmall.spi.statement.SStatement;
import com.thor.palmmall.spi.statement.SStatementQueryResult;
import com.thor.palmmall.spi.statement.SStatementService;
import com.thor.palmmall.spi.user.SAuthenInfo;
import com.thor.palmmall.spi.user.SPasswordInfo;
import com.thor.palmmall.spi.user.STenantUser;
import com.thor.palmmall.spi.user.SUser;
import com.thor.palmmall.spi.user.SUserService;

/**
 * @author NuoSa
 *
 * @since 1.0
 */
@Controller
@RequestMapping(value = "/statement")
public class StatementController {

  @Autowired
  private SUserService userService;
  @Autowired
  private SBasicService basicService;
  @Autowired
  private SStatementService statementService;

  @RequestMapping(value = "tologin.thor")
  public String ceshi() {
    return "/bill/login";
  }

  @RequestMapping(value = "tobill.thor")
  public String tobill() {
    return "/bill/bill";
  }

  @RequestMapping(value = "tobillDetail.thor")
  public String tobillDetail() {
    return "/bill/billDetail";
  }

  @RequestMapping(value = "tochangePass.thor")
  public String tochangePass() {
    return "/bill/changePass";
  }

  @RequestMapping(value = "tenantlogin.thor", method = RequestMethod.POST)
  @ResponseBody
  public STenantUser tenantlogin(SAuthenInfo authen) throws ThorServiceException {
    SResponse<STenantUser> sResponse = userService.tenantLogin(authen);
    if (sResponse.isSuccess()) {
      STenantUser user = sResponse.getBody();
      return user;
    } else {
      throw new ThorServiceException("登陆失败" + sResponse.getMessage());
    }
  }

  @RequestMapping(value = "marketlogin.thor", method = RequestMethod.POST)
  @ResponseBody
  public SUser marketlogin(SAuthenInfo authen) throws ThorServiceException {
    SResponse<SUser> sResponse = userService.marketLogin(authen);
    if (sResponse.isSuccess()) {
      SUser user = sResponse.getBody();
      return user;
    } else {
      throw new ThorServiceException("登陆失败：" + sResponse.getMessage());
    }
  }

  @RequestMapping(value = "query", method = RequestMethod.POST)
  @ResponseBody
  public SStatement query(SQueryByStoreParam storeParam, HttpServletRequest request) {
    return null;
  }

  @RequestMapping(value = "getBizType.thor")
  @ResponseBody
  public List<SBizType> getBizType(HttpServletRequest request) throws ThorServiceException {
    Boolean onlyUpper = Boolean.valueOf(request.getParameter("onlyUpper"));
    SResponse<List<SBizType>> sResponse = basicService.bizType(onlyUpper);
    if (sResponse.isSuccess()) {
      return sResponse.getBody();
    } else {
      throw new ThorServiceException(sResponse.getMessage());
    }
  }

  @RequestMapping(value = "getBuildings.thor")
  @ResponseBody
  public List<UCN> getBuildings(String buildParam, HttpServletRequest request)
      throws ThorServiceException, JsonParseException, JsonMappingException, IOException {
    ObjectMapper map = new ObjectMapper();
    SBuildingsParam param = map.readValue(buildParam, SBuildingsParam.class);
    SResponse<List<UCN>> sResponse = basicService.buildings(param);
    if (sResponse.isSuccess()) {
      return sResponse.getBody();
    } else {
      throw new ThorServiceException(sResponse.getMessage());
    }
  }

  @RequestMapping(value = "getFloors.thor")
  @ResponseBody
  public List<UCN> getFloors(String floorParam)
      throws ThorServiceException, JsonParseException, JsonMappingException, IOException {
    ObjectMapper map = new ObjectMapper();
    SFloorsParam param = map.readValue(floorParam, SFloorsParam.class);
    SResponse<List<UCN>> sResponse = basicService.floors(param);
    if (sResponse.isSuccess()) {
      return sResponse.getBody();
    } else {
      throw new ThorServiceException(sResponse.getMessage());
    }
  }

  @RequestMapping(value = "queryByStore.thor")
  @ResponseBody
  public SStatementQueryResult queryByStore(String queryParam)
      throws ThorServiceException, JsonParseException, JsonMappingException, IOException {
    ObjectMapper map = new ObjectMapper();
    SQueryByStoreParam param = map.readValue(queryParam, SQueryByStoreParam.class);
    SResponse<SStatementQueryResult> sResponse = statementService.queryByStore(param);
    if (sResponse.isSuccess())
      return sResponse.getBody();
    else
      throw new ThorServiceException(sResponse.getMessage());
  }

  @RequestMapping(value = "querybycontract.thor")
  @ResponseBody
  public SStatement querybycontract(String LoadParam)
      throws JsonParseException, JsonMappingException, IOException, ThorServiceException {
    ObjectMapper map = new ObjectMapper();
    SQueryByContractParam param = map.readValue(LoadParam, SQueryByContractParam.class);
    SResponse<SStatement> sResponse = statementService.queryByContract(param);
    if (sResponse.isSuccess()) {
      return sResponse.getBody();
    } else {
      throw new ThorServiceException(sResponse.getMessage());
    }
  }

  @RequestMapping(value = "changePassword.thor", method = RequestMethod.POST)
  @ResponseBody
  public Object changePassword(HttpServletRequest request) throws ThorServiceException {
    String id = request.getParameter("id");
    SPasswordInfo password = new SPasswordInfo();
    password.setNewPassword(request.getParameter("newPassword"));
    password.setOldPassword(request.getParameter("oldPassword"));

    SResponse<Object> sResponse = userService.changePassword(id, password);
    if (sResponse.isSuccess()) {
      Object obj = sResponse.getStatusCode();
      return obj;
    } else {
      throw new ThorServiceException("修改密码失败：" + sResponse.getMessage());
    }
  }

}
